package com.nbsystem.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbsystem.entity.ElcComment;

import com.nbsystem.service.ElcCommentService;
import com.nbsystem.util.MyWorldFilter;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (ElcComment)表控制层
 *
 * @author makejava
 * @since 2021-06-01 15:04:19
 */
@RestController
@RequestMapping("elcComment")
public class ElcCommentController {
    /**
     * 服务对象
     */
    @Resource
    private ElcCommentService elcCommentService;





    @RequestMapping("/list")
    public PageInfo<ElcComment> queryAll(ElcComment elcComment, Integer rows , Integer page){
        //分页组件
        PageHelper.startPage(page,rows);

//        标题模糊查询
        if (elcComment.getDetail() != null){
            elcComment.setDetail("%"+elcComment.getDetail()+"%");
        }

        if (elcComment.getTitle() != null){
            elcComment.setTitle("%"+elcComment.getTitle()+"%");
        }



        //查询所有信息
        List<ElcComment> list= elcCommentService.queryAll(elcComment);
        PageInfo<ElcComment> pageInfo = new PageInfo<>(list);
        return pageInfo;


    }


//    @RequestMapping("/add")
//    public String insertComment(ElcComment elcComment){
//        elcComment.setCreateTime(new Date());
//        elcComment.setUpdateTime(new Date());
//
//        elcCommentService.insert(elcComment);
//        return "success";
//    }


    @RequestMapping("query/{id}")
    public ElcComment queryById(@PathVariable Integer id){
        ElcComment elcComment = elcCommentService.queryById(id);

        return elcComment;
    }


    //    引用
    @Resource
    private MyWorldFilter myWorldFilter;

    @RequestMapping("update")
    public String updateComment(ElcComment elcComment){

        elcComment.setDetail(myWorldFilter.filterWorld(elcComment.getDetail()));

        elcComment.setUpdateTime(new Date());
        elcCommentService.update(elcComment);


        return "success";
    }



    @RequestMapping("delete/{id}")
    public String deleteComment(@PathVariable Integer id){
        elcCommentService.deleteById(id);
        return "success";

    }


    //    批量删除
    @RequestMapping("dels/{ids}")
    public String delComments(@PathVariable Integer[] ids){

        elcCommentService.deleteByIds(ids);
        return "success";
    }



    @RequestMapping("queryAll")
    public List<ElcComment> queryAll(ElcComment elcComment){

        return elcCommentService.queryAll(elcComment);
    }


}

